/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.person.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.vegetablefield.entity.Vegetablefield;
import com.jeesite.modules.vegetablefield.service.VegetablefieldService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.person.entity.PersonFarmmanagement;
import com.jeesite.modules.person.service.PersonFarmmanagementService;

/**
 * js_person_farmmanagementController
 * @author 达芬奇
 * @version 2019-10-05
 */
@Controller
@RequestMapping(value = "${adminPath}/person/personFarmmanagement")
public class PersonFarmmanagementController extends BaseController {

	@Autowired
	private PersonFarmmanagementService personFarmmanagementService;

	@Autowired
	private VegetablefieldService vegetablefieldService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public PersonFarmmanagement get(String id, boolean isNewRecord) {
		return personFarmmanagementService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("person:personFarmmanagement:view")
	@RequestMapping(value = {"list", ""})
	public String list(PersonFarmmanagement personFarmmanagement, Model model) {
		model.addAttribute("personFarmmanagement", personFarmmanagement);
		return "modules/person/personFarmmanagementList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("person:personFarmmanagement:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<PersonFarmmanagement> listData(PersonFarmmanagement personFarmmanagement, HttpServletRequest request, HttpServletResponse response) {
		personFarmmanagement.setPage(new Page<>(request, response));
		Page<PersonFarmmanagement> page = personFarmmanagementService.findPage(personFarmmanagement);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("person:personFarmmanagement:view")
	@RequestMapping(value = "form")
	public String form(PersonFarmmanagement personFarmmanagement, Model model) {
		model.addAttribute("personFarmmanagement", personFarmmanagement);
		return "modules/person/personFarmmanagementForm";
	}

	/**
	 * 保存个人田地管控
	 */
	@RequiresPermissions("person:personFarmmanagement:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated PersonFarmmanagement personFarmmanagement) {
		personFarmmanagementService.save(personFarmmanagement);
		return renderResult(Global.TRUE, text("保存个人田地管控成功！"));
	}
	
	/**
	 * 删除个人田地管控
	 */
	@RequiresPermissions("person:personFarmmanagement:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(PersonFarmmanagement personFarmmanagement) {
		Vegetablefield vegetablefield =vegetablefieldService.get(personFarmmanagement.getFid());
		vegetablefield.setArea(vegetablefield.getArea()+personFarmmanagement.getFsize());
		vegetablefieldService.save(vegetablefield);
		personFarmmanagementService.delete(personFarmmanagement);
		return renderResult(Global.TRUE, text("归还个人田地成功！"));
	}
	
}